package com.yc.ui.admin;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.layout.FillLayout;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.ui.Order;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class MaInfoUI extends Composite {
	private Text text_pwd;
	private Text text_name;
	private Table table;
	private String filePath = ""; //选中的图片路径
	private Composite com_image;
	GoodsIDao dao = new GoodsDao();
	byte [] bt;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MaInfoUI(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约1.PNG") );
		SWTUtil.imagetSize(composite);		
		text_pwd = new Text(composite, SWT.BORDER);
		text_pwd.setBounds(394, 364, 100, 23);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setBounds(244, 301, 100, 23);
		lblNewLabel_1.setText("管理员账号");
		
		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(244, 367, 92, 23);
		lblNewLabel_2.setText("管理员密码");
		
		text_name = new Text(composite, SWT.BORDER);
		text_name.setBounds(394, 301, 100, 23);
		
		Label lblNewLabel_4 = new Label(composite, SWT.NONE);
		lblNewLabel_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 13, SWT.BOLD));
		lblNewLabel_4.setAlignment(SWT.CENTER);
		lblNewLabel_4.setBounds(610, 482, 147, 38);
		lblNewLabel_4.setText("致力于企业");
		
		Label lblNewLabel_5 = new Label(composite, SWT.NONE);
		lblNewLabel_5.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 13, SWT.BOLD));
		lblNewLabel_5.setBounds(795, 534, 147, 52);
		lblNewLabel_5.setText("服务于顾客");
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = text_name.getText().toString().trim();
				String pwd = text_pwd.getText().toString().trim();
				byte[] image = getImageByte(filePath);
				if(StringUtil.checkNull(name, pwd)) {
					return;
				}
				int n = dao.addAdmin(name, pwd, image);
				if(n > 0) {
					SWTUtil.showMessage(getShell(), "添加成功！！！");
					showMessage();
				}else {
					SWTUtil.showMessage(getShell(), "添加失败！！！");
					return ;
				}
			}
		});
		btnNewButton.setBounds(432, 463, 98, 30);
		btnNewButton.setText("添加管理员");
		
		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLocation(65, 25);
		table.setSize(947, 225);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(177);
		tblclmnNewColumn.setText("管理员编号");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(165);
		tblclmnNewColumn_1.setText("管理员姓名");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(169);
		tblclmnNewColumn_2.setText("管理员头像");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(150);
		tblclmnNewColumn_3.setText("管理员头像");
		
		com_image = new Composite(composite, SWT.NONE);
		com_image.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		com_image.setBounds(695, 295, 142, 152);
		
		Label lab = new Label(composite, SWT.NONE);
		lab.setBounds(556, 301, 92, 20);
		lab.setText("管理员头像");
		
		com_image.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				//上传文件的框
				FileDialog fd = new FileDialog(getShell());
				//默认路径
				fd.setFilterPath("SystemRoot");
				//限定上传图片格式
				fd.setFilterExtensions(new String[] {"*.*", "*.jdp", "*.png", "*.gif"});
				String select = fd.open();
				if(StringUtil.checkNull(select)) {
					SWTUtil.showMessage(getShell(), "请选择你要上传的图片！！！");
					return;
				}
				//将选中的图片的绝对路径储存到全局变量
				filePath = select;
				try {
					File file = new File(select);
					//将图片设置到com_image
					//Image image = new Image(Display.getCurrent(), new FileInputStream(file));
					//com_image.setBackgroundImage(image);
					//调用封装后的工具类设置 自适应封面背景
					com_image.setBackgroundImage(SWTUtil.imageSize(com_image, new FileInputStream(file)));
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		showMessage();
	}
	
	public void showMessage() {
		//清除表格
		table.removeAll();
		//查询信息
		List<Map<String, Object>> list = dao.findAdmin();
		
		TableItem tableItem = null;//表格一行记录
		//判空
		if(null == list || list.isEmpty()) {
			SWTUtil.showMessage(getShell(), "查无管理员信息");
			return;
		}
		//有信息 循环
		
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
					StringUtil.toString(m.get("a_id")),
					StringUtil.toString(m.get("a_name")),
					StringUtil.toString(m.get("a_pwd")),
			});
			bt = ( byte[] )m.get("a_image");
			// 字节数组判空
			if( null != bt) {
				Image image = SWTUtil.imageSize(100, 50, new ByteArrayInputStream(bt));
				tableItem.setImage(3, image);
			}
		}
	}
	
	protected byte[] getImageByte(String filePath) {
		byte [] bimage = null;
		//存入图片
		if(!StringUtil.checkNull(filePath)) {
			File file = new File(filePath);
			try(InputStream in = new FileInputStream(file)) {
				byte[] bt = new byte[(int) file.length()];
				while(in.read(bt) != -1) {
					//存入字节数组
					bimage = bt;
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return bimage;
	}
}
